package com.devin.controller;

import com.devin.pojo.*;
import com.devin.service.OrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/getOrders")
    public ModelAndView getOrders(@RequestParam(value = "page",defaultValue = "1")Integer page
            ,@RequestParam(value = "size",defaultValue = "5")Integer size){
        List<Order> orders = orderService.getOrders(page, size);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("orderPageInfo",orderPageInfo);
        modelAndView.setViewName("pages/order-pages-list");
        return modelAndView;
    }

    @RequestMapping(value = "/toAddOrder",method = RequestMethod.GET)
    public ModelAndView toAddOrder(){
        List<Product> products=orderService.toAddOrder();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("products",products);
        modelAndView.setViewName("pages/order-add");
        return modelAndView;
    }
    @RequestMapping("/addOrder")
    @ResponseBody
    public String addOrder(@RequestBody OrderAndTraveller orderAndTraveller){
       orderService.addOrder(orderAndTraveller);
       return "done";
    }

    @RequestMapping("/getOrderInfo")
    public ModelAndView getOrderInfo(Integer orderId){
        Order order =orderService.getOrderInfo(orderId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("order",order);
        modelAndView.setViewName("pages/order-details");
        return modelAndView;
    }
    @RequestMapping(value = "/delOrder",method = RequestMethod.GET)
    public String delOrder(Integer orderId,Integer memberId){
        orderService.delOrder(orderId,memberId);
        return "redirect:/orders/getOrders";
    }
    @RequestMapping(value = "/delOrdersChecked",method = RequestMethod.POST)
    @ResponseBody
    public String delOrdersChecked(Integer[] ids){
        orderService.delOrdersChecked(ids);
        return "good";
    }
}
